package com.helwen.tools;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 将最多四层结构的xml解析成`/root/message/a/b`  C:\develop\docs\xml\genarateXmlPath.txt
 */
public class XmlParseTool {

    //只对元素值处理，解析xml文件中的每个元素。
    @Test
    public void xmlParse() throws FileNotFoundException, Exception {

        String parentTemp="message";
        //1.创建Reader对象
        SAXReader reader = new SAXReader();
        //2.加载xml
        Document document = reader.read(new File("src/main/resources/com/template/demo.xml"));
        //3.获取根节点
        Element rootElement = document.getRootElement();
        String name = rootElement.getName();
        boolean b=false;

        Iterator iterator = rootElement.elementIterator();
        File file=new File("C:\\develop\\docs\\xml\\genarateXmlPath.txt");//输入格式放在这个目录下
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream, "UTF-8");
        List<String> list=new ArrayList<>();//检查是否有重复的节点。
        while (iterator.hasNext()){
            Element stu = (Element) iterator.next();
            String twoLevel=stu.getName();
            /*List<Attribute> attributes = stu.attributes();
            System.out.println("======获取属性值======");
            for (Attribute attribute : attributes) {
                System.out.println(attribute.getValue());
            }*/
            boolean ishas=false;
            if(!stu.hasMixedContent()){
                osw.write("`"+stu.getPath()+"`\r\n");
                osw.flush();
            }else {
                if(list.size()>1){
                    for(String tname:list){
                        if(tname.equals(twoLevel)){
                            ishas=true;
                        }
                    }
                }
            }
            if(!ishas){
                list.add(twoLevel);
                Iterator iterator1 = stu.elementIterator();
                List<String> erlist=new ArrayList<>();//检查是否有重复的节点。
                while (iterator1.hasNext()){
                    Element stuChild = (Element) iterator1.next();
                    boolean isEr=false;
                    if(!stuChild.hasMixedContent()){
                        String path = stuChild.getPath();
                        osw.write("`"+path+"`,\r\n");
                        osw.flush();
                        System.out.println("path:==========="+path+"===="+stuChild.getPath());
                        System.out.println("节点名："+stuChild.getName()+"---节点值："+stuChild.getStringValue());
                    }else {
                        if(erlist.size()>1){
                            for(String tname:erlist){
                                if(tname.equals(twoLevel)){
                                    isEr=true;
                                }
                            }
                        }
                        if(!isEr){
                            Iterator iterator2 = stuChild.elementIterator();
                            while (iterator2.hasNext()) {
                                Element thirdChild = (Element) iterator2.next();
                                String path = thirdChild.getPath();
                                osw.write("`" + path + "`,\r\n");
                                osw.flush();
                            }
                        }
                    }

                }
            }

        }

        osw.close();
        fileOutputStream.close();
    }

}
